package com.omkarpatil.learningjpa2.mapping.onetoone.fk.bidi;

import javax.persistence.EntityManager;

import com.omkarpatil.learningjpa2.util.EntityManagerFactoryFactory;

public class TestDriver {

	public static void main(String[] args) {
		/* Creating a person */
		EntityManager em1 = EntityManagerFactoryFactory.getEntityManagerFactory().createEntityManager();
		em1.getTransaction().begin();
		
		System.out.println("##### Adding desk to database ...");
		Desk desk = new Desk("I6PL06");
	
		System.out.println("##### Adding Employee to database ...");
		Employee emp1 = new Employee("Omkar Patil", desk);
		desk.setEmployee(emp1);
		
		em1.persist(desk);
		em1.persist(emp1);
		
		em1.getTransaction().commit();
		em1.close();
		
		int deskId = desk.getId();
		
		System.out.println("##### Looking up desk to find employee assigned to it ...");
		EntityManager em2 = EntityManagerFactoryFactory.getEntityManagerFactory().createEntityManager();
		em2.getTransaction().begin();
		
		Desk desk1 = em2.find(Desk.class, deskId);
		
		System.out.println("##### Finding out who's assgined to desk " + desk1.getNumber());
		System.out.println("##### It's " + desk1.getEmployee().getName());
		

		em2.getTransaction().commit();
		em2.close();
	}

}
